583. Delete Operation for Two Strings - LeetCode Solution


String

Python Code:

class Solution:
    def minDistance(self, a: str, b: str) -> int:
        		
        t = []
        
        for i in range(len(a) + 1):
        
            l = []
            for j in range(len(b) + 1):
                l.append(0)
            t.append(l)
        
        
        
        for i in range(1, len(a) +1, 1):
            for j in range(1, len(b) + 1, 1):
                if a[i-1] == b[j-1]:
                    t[i][j] = 1 + t[i-1][j-1]
                else:
                    t[i][j] = max(t[i][j-1], t[i-1][j])
        
        
        return len(a) + len(b) -  (t[len(a)][len(b)] * 2)
        


Comments

Submit
0 Comments
More Questions

400B - Inna and New Matrix of Candies
870B - Maximum of Maximums of Minimums
1600E - Array Game
1149A - Prefix Sum Primes
277A - Learning Languages
769A - Year of University Entrance
1738A - Glory Addicts
1738C - Even Number Addicts
1064B - Equations of Mathematical Magic
384A - Coder
1738B - Prefix Sum Addicts
1352D - Alice Bob and Candies
1316D - Nash Matrix
1548B - Integers Have Friends
348A - Mafia
315B - Sereja and Array
204A - Little Elephant and Interval
385B - Bear and Strings
114C - Grammar Lessons
1427A - Avoiding Zero
583A - Asphalting Roads
1358B - Maria Breaks the Self-isolation
828A - Restaurant Tables
1735A - Working Week
1735D - Meta-set
1735B - Tea with Tangerines
1735C - Phase Shift
1321C - Remove Adjacent
281B - Nearest Fraction
1043A - Elections